package com.car.comm;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class Util {
	public static byte[] toByteArray(InputStream input) {
		if(input == null) {
			return null;
		}
		ByteArrayOutputStream output = null;
		byte[] result = null;
		try{
			output = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];
			int n = 0;
			while (-1 != (n = input.read(buffer))) {
				output.write(buffer, 0, n);
			}
			result = output.toByteArray();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeQuietly(input);
			closeQuietly(output);
		}
		return result;
	}
	
	public static void closeQuietly(InputStream is) {
		try {
			if(is != null) {
				is.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void closeQuietly(OutputStream os) {
		try{
			if(os != null) {
				os.close();
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
